home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1258 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  87 lines

  1. Path: solon.com!not-for-mail
  2. From: a1s@ix.netcom.com (Andrew Snyder)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Leading and Trailing Blanks
  5. Date: 12 Jan 1996 11:55:29 -0600
  6. Organization: Netcom
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4d67ah$1o1@solutions.solon.com>
  10. References: <4chh1b$685@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-NETCOM-Date: Fri Jan 12  8:49:06 AM PST 1996
  13. X-Newsreader: Forte Free Agent 1.0.82
  14.  
  15. mskc@io.com (Casey Claiborne) wrote:
  16.  
  17.  
  18. >Hello -
  19. >    I am wondering if anyone out there has a program (or knows of one)
  20. >that allows one to strip leading and trailing blanks from a string. 
  21. >ex:
  22.  
  23. >    char test[20];
  24. >    strcpy(test,"  TESTING  ");
  25. >    printf("%s", test);
  26.  
  27. >will produce an output like
  28. >  TESTING  
  29.  
  30. >that has blanks at the beginning of "TESTING". I would like to 
  31. >have the following result
  32.  
  33. >TESTING
  34.  
  35. >that has no leading blanks.
  36.  
  37. >I would *greatly* appreciate any type of help or hints in working with
  38. >this.
  39.  
  40. >I'll be looking out here, but e-mails are also appreciated :)
  41.  
  42. >TIA
  43.  
  44. >Casey
  45.  
  46. I like this aproach
  47.  
  48. char *strip(char *s)
  49. {
  50.     char *start, *end, *p;
  51.  
  52.     start=NULL;
  53.     end = NULL;
  54.     for(p = s; *p; p++)
  55.     {
  56.         if(!isspace(*p))
  57.         {
  58.             if(!start)
  59.                 start = p;
  60.             else
  61.                 end = p;
  62.         }
  63.     }
  64.  
  65.     /* p now pointes to trailling null */
  66.  
  67.     if(start)
  68.     {
  69.         if(end)
  70.         {
  71.             memmove(s,start,(end - start) + 1);
  72.                                         /* +1 for the fence post */
  73.             s[(end - start) + 1] = '\0';
  74.             /* add that null */
  75.         }
  76.         else
  77.         {
  78.             memmove(s,start,(end - start) + 2);
  79.              /* +2 gets the trailing null */
  80.         }
  81.     }
  82.     return s;
  83. }
  84. Andrew Snyder
  85. a1s@ix.netcom.com
  86. char disclaimer[] = {'\0'}; /* I'm on cash net */
  87.